angular.module('app')
    .factory('AuthInterceptor', ['$window', '$location','$log',
        function ($window, $location,$log) {
            return {
                'request': function (config) {
                    var whiteUrlArr=["tpl/user/page_signin.html","tpl/user/page_signup.html","tpl/user/page_forgotpwd.html","tpl/blocks/page_footer.html","/user/registeraccount"];
                    if(whiteUrlArr.indexOf(config.url)!=-1 ){
                        return config;
                    }
                    config.headers = config.headers || {};
                    var userInfo = $window.localStorage["userInfo"];
                    if (!!!userInfo) {
                        //跳转到登录窗口
                        $location.path('/access/signin');
                        return config;
                    }
                    var user = JSON.parse(userInfo);
                    config.headers.accessToken = user.accessToken;
                    config.headers.userName = user.username;
                    return config;
                },
                'responseError': function (response) {
                    if(response.status==401){
                        $location.path('/access/signin');
                    }
                    $log.error(response);
                },
                'response': function (response) {
                    //返回

                    return response;
                }
            };
    }]);

